import { Controller, Get, Query } from '@nestjs/common';
import { ResponseInterface } from '../common/response/response.interface';
import { SuccessResponse } from '../common/response/success-response';
import { ErrorResponse } from '../common/response/error-response';
import { TasksService } from './tasks.service';

@Controller('tasks')
export class TasksController {
  constructor(private readonly tasksService: TasksService) {}

  // 新建一个定时任务
  @Get('cron/add')
  async addCronJob(@Query() query): Promise<ResponseInterface> {
    try {
      await this.tasksService.addCronJob(query.name, query.seconds);
      return new SuccessResponse({});
    } catch (e: any) {
      return new ErrorResponse(e.message);
    }
  }

  // 停止一个定时任务
  @Get('cron/stop')
  async stopCronJob(@Query() query) {
    try {
      await this.tasksService.stopCronJob(query.name);
      return new SuccessResponse({});
    } catch (e: any) {
      return new ErrorResponse(e.message);
    }
  }

  // 删除一个定时任务
  @Get('cron/del')
  async delCronJob(@Query() query) {
    try {
      await this.tasksService.delCronJob(query.name);
      return new SuccessResponse({});
    } catch (e: any) {
      return new ErrorResponse(e.message);
    }
  }
}
